import { createRouter, createWebHistory } from "vue-router";
import Index from "../views/index/index.vue";
import { useSystemStore } from "@/store/system/systemStore.js";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/index",
      name: "index",
      component: Index,
      children: [
        {
          // 设置此子页面为主页面
          path: "",
          name: "profile",
          component: () => import("../views/index/profile/profile.vue"),
        },
        {
          path: "/index/contribute",
          component: () => import("../views/index/contribute/contribute.vue"),
        },
        {
          path: "/index/comment",
          component: () => import("../views/index/comment/comment.vue"),
        },
        {
          path: "/index/contributejudge",
          component: () =>
            import("../views/index/contributejudge/contributejudge.vue"),
        },
        {
          path: "/index/commentjudge",
          component: () =>
            import("../views/index/commentjudge/commentjudge.vue"),
        },
        {
          path: "/index/user",
          component: () => import("../views/index/user/user.vue"),
        },
        {
          path: "/index/properties",
          component: () => import("../views/index/properties/properties.vue"),
        },
      ],
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/login/login.vue"),
    },
    {
      path: "/test",
      name: "test",
      /*
      写法区分
       */
      // component: import("../views/test/test.vue")
      component: () => import("../views/test/test.vue"),
    },
    {
      path: "/temp",
      name: "temp",
      /*
      写法区分
       */
      // component: import("../views/test/test.vue")
      component: () => import("../views/test/temp.vue"),
    },
    {
      path: "/tlogin",
      name: "tlogin",
      component: () => import("../views/test/test-login.vue"),
    },
    // 将/重定向到test
    {
      path: "/",
      redirect: "/index",
    },
  ],
});

/**
 * 路由守卫
 */
router.beforeEach(async (to, from, next) => {
  // 访问连接后立即执行的代码
  const systemStore = useSystemStore();
  // MARK 系统初始化完成之后才进行后续的响应
  await systemStore.systemDictionaryInit();
  next();
});

export default router;
